home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / Jooky / lockfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-01  |  1.1 KB  |  53 lines

  1. #include "defines.h"
  2. #include "includes.h"
  3. #include "funcs.h"
  4.  
  5. int createlockfile()
  6. {
  7.     int flockstat;
  8.     char *pidstring;
  9.  
  10.     pidstring=calloc(1,256);
  11.     if (open((const char *)LOCKFILE,O_RDONLY)==-1)
  12.         lockfilehan=open((const char *)LOCKFILE,\
  13.             O_EXCL|O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
  14.     else
  15.         lockfilehan=open((const char *)LOCKFILE,\
  16.             O_EXCL,S_IRUSR|S_IWUSR);
  17.  
  18.     flockstat=flock(lockfilehan,LOCK_EX|LOCK_NB);
  19.  
  20.     if (flockstat==0)
  21.     {
  22.         sprintf(pidstring,"%d",getpid());
  23.         write (lockfilehan,pidstring,strlen(pidstring));
  24.     }
  25.  
  26.     return flockstat;
  27. }
  28.  
  29. void removelockfile(void)
  30. {
  31.     if (playerpid != 0) kill(playerpid,SIGTERM);
  32.     if (counterpid != 0) kill(counterpid,SIGKILL);
  33.     if (futabapid != 0) kill(futabapid,SIGKILL);
  34.     if (socketpid != 0) {
  35.         shutdown(fd,2);
  36.         kill(socketpid,SIGKILL);
  37.     }
  38.     close(lockfilehan);
  39.     unlink((const char *)LOCKFILE);
  40.     if (dirhasbeenloaded==1) cleanup();
  41. /*    if (((daemonize==0)&&(playerpid!=0)&&(counterpid!=0)) || (otest==(FILE *)NULL)) */
  42.     if (((daemonize==0)&&(playerpid!=0)&&(counterpid!=0)))
  43.     {
  44.         clear();
  45.         nocbreak();
  46.         echo();
  47.         nl();
  48.         curs_set(oldcurs);
  49.         endwin();
  50.     }
  51.     exit(0);
  52. }
  53.